﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Web.Mvc;
using KonsolAntresol.Models;
using Resources;

namespace KonsolAntresol.Controllers
{
	public class HomeController : Controller
	{
		//
		// GET: /Home/

		#region Actions

		public ActionResult Index()
		{
			ViewBag.DesignProjectTypes = PrepareList();
			return View();
		}

		public ActionResult Prices(string pageName)
		{
			ViewBag.PageName = string.IsNullOrEmpty(pageName) ? "DesignProject" : pageName;
			return View("SubServicesAndPrices");
		}

		public ActionResult Portfolio()
		{
			return View();
		}

		public ActionResult Feedbacks()
		{
			return View();
		}

		public ActionResult Contacts(string pageName)
		{
			ViewBag.PageName = pageName;
			return View();
		}

		public ActionResult AboutDesign()
		{
			return View();
		}

		public ActionResult Decoration()
		{
			return View();
		}

		public ActionResult Kitchens()
		{
			return View();
		}

		public ActionResult Furniture()
		{
			return View();
		}

		public ActionResult Repair()
		{
			return View();
		}

		public ActionResult Library()
		{
			return View();
		}

		[HttpGet]
		public ActionResult GetFile(string fileName)
		{
			var extension = fileName.Split('.').Last();
			var path = Server.MapPath(string.Format("~/Files/{0}", fileName));
			switch (extension)
			{
				case "pdf":
					return File(path, MediaTypeNames.Application.Pdf);
				case "jpg":
				case "jpeg":
					return File(path, MediaTypeNames.Image.Jpeg);
				default:
					throw new ArgumentException("Unknown extension of the file.");
			}
		}

		[HttpGet]
		public JsonResult GetGalleryImages()
		{
			const string relativeUrl = "/Content/images/galleryImages/";
			var images = Directory.GetFiles(Server.MapPath(relativeUrl))
				.Select(x=>string.Format("{0}{1}", relativeUrl, Path.GetFileName(x)));
			return Json(images, JsonRequestBehavior.AllowGet);
		}

		[HttpGet]
		public JsonResult GetPortfolioImages()
		{
			const string relativeUrl = "/Content/images/portfolioImages/";
			var images = Directory.GetFiles(Server.MapPath(relativeUrl))
				.Select(x => string.Format("{0}{1}", relativeUrl, Path.GetFileName(x)));
			return Json(images, JsonRequestBehavior.AllowGet);
		}

		#endregion

		#region Private methods

		private IEnumerable<SelectListItem> PrepareList()
		{
			return new List<SelectListItem>
			{
				new SelectListItem
				{
					Text = Resource.Base,
					Value = Resource.Base
				},
				new SelectListItem
				{
					Text = Resource.Standart,
					Value = Resource.Standart
				},
				new SelectListItem
				{
					Text = Resource.Full,
					Value = Resource.Full
				}
			};
		}

		private IEnumerable<MenuItemModel> PrepareMenu()
		{
			var subMenu = new List<MenuItemModel>
			{
				new MenuItemModel{ Text = Resource.DesignProject, Controller = "Home", Action = "Prices", Selected = false, PageName = "DesignProject"},
				new MenuItemModel{ Text = Resource.GradeObject, Controller = "Home", Action = "Prices", Selected = false, PageName = "GradeObject"},
				new MenuItemModel{ Text = Resource.ProjectControl, Controller = "Home", Action = "Prices", Selected = false, PageName = "ProjectControl"},
				new MenuItemModel{ Text = Resource.RepairWorks, Controller = "Home", Action = "Prices", Selected = false, PageName = "RepairWorks"},
				new MenuItemModel{ Text = Resource.KitchenProduction, Controller = "Home", Action = "Prices", Selected = false, PageName = "KitchenProduction"}
			};
			return new List<MenuItemModel>
			{
				new MenuItemModel{ Text = Resource.Home, Controller = "Home", Action = "Index", Selected = true },
				new MenuItemModel{ Text = Resource.Prices, Controller = "Home", Action = "Prices", Selected = false, SubMenu = subMenu },
				new MenuItemModel{ Text = Resource.Portfolio, Controller = "Home", Action = "Portfolio", Selected = false },
				new MenuItemModel{ Text = Resource.Feedbacks, Controller = "Home", Action = "Feedbacks", Selected = false },
				new MenuItemModel{ Text = Resource.Contacts, Controller = "Home", Action = "Contacts", Selected = false, PageName = "Contacts"}
			};
		}

		#endregion

		protected override void OnActionExecuted(ActionExecutedContext filterContext)
		{
			ViewBag.MenuItems = PrepareMenu();
		}
	}
}
